<?php
// Get an idea
//
if (!class_exists('fx_control')) {
	require_once ('./application/library/html/fx_control.php');
} 
class fx_filter extends fx_control
{
	// Model Class
	protected $_model;
	// Array
	protected $_filters;
	
	public function __construct() 
	{
		parent::__construct();
	}
	
	// Main Method
	public function filter($filters, $model = null)
	{
		if (!is_array($filters) || count($filters) == 0) {
			return $this;
			echo 1;
			//throw new Exception("Filter collection must be array and not empty");
		}
		if (!is_null($model)) {
			$this->setModel($model);
		} else {
			if (is_null($this->_model)) {
				return $this;
			}
		}
		$model =& $this->getModel();
		foreach ($filters as $field => $data) {
			if (!is_array($data['value']) && strlen($data['value']) > 0) {
				$data['value'] = trim($data['value']);
				switch ($data['type']) {
					case 'equal':
						// Neu khac gia tri ngoai le (vd: = 0)
						// Gia tri ngoai le la nhung gia tri duoc dat ra ma khong duoc phep filter
						if (isset($data['exception'])) {
							if ($data['value'] != $data['exception'])
							{
								$model->addAttributeToFilter($field, $data['value'], '=');
							}
						} else {
							$model->addAttributeToFilter($field, $data['value'], '=');
						}
						break;
					case 'string':
						$model->addAttributeToFilter($field, '%'.$data['value'].'%', 'like');	
						break;
					case 'startString':
						$model->addAttributeToFilter($field, '%'.$data['value'], 'like');	
						break;
					case 'endString':
						$model->addAttributeToFilter($field, $data['value'].'%', 'like');	
						break;
					default:
						$model->addAttributeToFilter($field, $data['value'], '=');
						break;
				}
			} else {
				switch ($data['type']) {
					case 'range':
						if (strlen(trim($data['value']['from'])) > 0) 
						{
							$model->addAttributeToFilter($field, $data['value']['from'], '>=');	
						}						
						if (strlen(trim($data['value']['to'])) > 0)
						{
							$model->addAttributeToFilter($field, $data['value']['to'], '<=');
						}
					break;
				}
			}
			$this->_filters[$field] = $data;
		}
		return $this;
	}
	
	public function getFilter()
	{
		return $this->_filters;
	}
	
	public function setFilter($_filters)
	{
		$this->_filters = $_filters;
		return $this;
	}
	
	public function getModel()
	{
		$model =& $this->_model;
		return $model;
	}
	
	public function setModel($model)
	{
		$this->_model =& $model;
		return $this;
	}
	
	public function getValue($name)
	{
		if (!isset($this->_filters[$name])) {
			return null;
		}
		return $this->_filters[$name]['value'];
	}
	
	public function getValueFrom($name) {
		$range = $this->getValue($name);
		if (is_array($range)) {
			return $range['from'];
		}
		return null;
	}
	
	public function getValueTo($name) {
		$range = $this->getValue($name);
		if (is_array($range)) {
			return $range['to'];
		}
		return null;
	}
} 